package com.tusuo.stds.ui.questionbank;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Point;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.tusuo.stds.R;

import java.util.ArrayList;
import java.util.List;


public class SettingsTransparentTempDialog extends Dialog {

    private static int sLastIndex;

    public SettingsTransparentTempDialog(final Activity activity, final QuestionBankTempPresenter presenter) {
        super(activity, R.style.TranslucentDialog);

        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay(); // 获取屏幕宽、高用
        Point point = new Point();
        display.getSize(point);

        // 去掉隐藏的title区域，不然会被点击到
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams wmlp = getWindow().getAttributes();
        wmlp.width = (int) (point.x * 0.8);    // 宽度占屏幕比例
        wmlp.height = (int) (point.y * 0.8);    // 高度占屏幕比例
        wmlp.gravity = Gravity.CENTER;
        getWindow().setAttributes(wmlp);

        // 设置内容
        setContentView(R.layout.question_bank_settings_dialog);

        setTitle(null);
        setCancelable(true);
        setOnCancelListener(null);

        sLastIndex = 0;

        RelativeLayout layout0 = (RelativeLayout) findViewById(R.id.question_bank_settings_layout_select_0);
        RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.question_bank_settings_layout_select_1);
        RelativeLayout layout2 = (RelativeLayout) findViewById(R.id.question_bank_settings_layout_select_2);
        RelativeLayout layout3 = (RelativeLayout) findViewById(R.id.question_bank_settings_layout_select_3);
        RelativeLayout layout4 = (RelativeLayout) findViewById(R.id.question_bank_settings_layout_select_4);
        RelativeLayout layout5 = (RelativeLayout) findViewById(R.id.question_bank_settings_layout_select_5);
        RelativeLayout layout6 = (RelativeLayout) findViewById(R.id.question_bank_settings_layout_select_6);
        RelativeLayout layout7 = (RelativeLayout) findViewById(R.id.question_bank_settings_layout_select_7);
        RelativeLayout layout8 = (RelativeLayout) findViewById(R.id.question_bank_settings_layout_select_8);

        final List<RelativeLayout> layoutList = new ArrayList<>();
        layoutList.add(layout0);
        layoutList.add(layout1);
        layoutList.add(layout2);
        layoutList.add(layout3);
        layoutList.add(layout4);
        layoutList.add(layout5);
        layoutList.add(layout6);
        layoutList.add(layout7);
        layoutList.add(layout8);

        for (int i = 0; i < layoutList.size(); i++) {
            layoutList.get(i).setTag(i);
        }

        ImageView ivSelect0 = (ImageView) findViewById(R.id.question_bank_settings_iv_select_0);
        ImageView ivSelect1 = (ImageView) findViewById(R.id.question_bank_settings_iv_select_1);
        ImageView ivSelect2 = (ImageView) findViewById(R.id.question_bank_settings_iv_select_2);
        ImageView ivSelect3 = (ImageView) findViewById(R.id.question_bank_settings_iv_select_3);
        ImageView ivSelect4 = (ImageView) findViewById(R.id.question_bank_settings_iv_select_4);
        ImageView ivSelect5 = (ImageView) findViewById(R.id.question_bank_settings_iv_select_5);
        ImageView ivSelect6 = (ImageView) findViewById(R.id.question_bank_settings_iv_select_6);
        ImageView ivSelect7 = (ImageView) findViewById(R.id.question_bank_settings_iv_select_7);
        ImageView ivSelect8 = (ImageView) findViewById(R.id.question_bank_settings_iv_select_8);

        final List<ImageView> ivList = new ArrayList<>();
        ivList.add(ivSelect0);
        ivList.add(ivSelect1);
        ivList.add(ivSelect2);
        ivList.add(ivSelect3);
        ivList.add(ivSelect4);
        ivList.add(ivSelect5);
        ivList.add(ivSelect6);
        ivList.add(ivSelect7);
        ivList.add(ivSelect8);

        for (ImageView iv : ivList) {
            iv.setVisibility(View.GONE);
        }

        int number = presenter.getQuestionSettingCorrectCount();
        if (number > -1 && number < 9) {
            sLastIndex = number;
            ivList.get(number).setVisibility(View.VISIBLE);
        }

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = (int) v.getTag();

                // api
                ivList.get(sLastIndex).setVisibility(View.GONE);
                ivList.get(index).setVisibility(View.VISIBLE);
                sLastIndex = index;

                presenter.setQuestionSettingCorrectCount(index);
            }
        };

        for (RelativeLayout layout : layoutList) {
            layout.setOnClickListener(listener);
        }
    }
}
